package org.gen.pojo.util;

import org.gen.pojo.config.GenConfig;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created by zhuge on 2017/10/11.
 */
public class DBMetaUtil {
    private static DatabaseMetaData dbmd;
    static {
        try {
            Connection connection = getConnection();
            //获取数据库源信息
            dbmd = connection.getMetaData();

        } catch (Exception e) {
            throw new RuntimeException("数据库连接异常");
        }

    }
    //获取数据库源信息
    public static DatabaseMetaData getMetaData() {
        return dbmd;
    }
    //获取数据库源连接
    public static Connection getConnection(){

        try {
            //加载数据库驱动
            Class.forName(GenConfig.driverClassName);
            //获取连接
            Connection connection = DriverManager.getConnection(GenConfig.jdbcurl, GenConfig.username, GenConfig.password);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
